<template>
  <view>
    <view class="search-box">
      <uni-search-bar :radius="100" cancelButton=none focus="true" @input="input" ></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResults.length !==0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
        <view class="goods_name">{{item.goods_name}}</view>
          <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索</text>
        <!-- 删除图标 -->
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" type="error"  v-for="(item,i) in history" :key='i' @click="togoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 延时器
        timer:null,
        // 搜索的关键词
        kw:'',
        // 搜索的结果列表
        searchResults:[],
        // 搜索历史的数组
        historyList:[]
      }
    },
    onLoad() {
     this.historyList= JSON.parse(uni.getStorageSync("kw") || '[]')
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
       this.timer= setTimeout(()=>{
          this.kw=e
          this.getsearchList()
        },500)
      },
    async  getsearchList(){
        // 判断搜索词是否为空
        if(this.kw.length ===0){
          this.searchResults=[]
          return 
        }
      const {data:res} =await uni.$http.get('/goods/qsearch',{query:this.kw})
      console.log(res)
      if(res.meta.status !==200) return uni.$showMessage()
      this.searchResults=res.message
      this.saveSearchHistory()
      },
      // 保存搜索历史记录
      saveSearchHistory(){
        // 要解决前后顺序(方法一)
        // this.historyList.push(this.kw)
        // 搜索关键字重复的问题1
        // this.historyList= [...new Set(this.historyList)]
        // this.historyList.push(this.kw)
        // 要解决前后顺序和搜索关键字重复的问题
        // 搜索关键字重复的问题2
       const set= new Set(this.historyList)
       set.delete(this.kw)
       set.add(this.kw)
       this.historyList=Array.from(set)
       console.log(set)
       // 对搜索历史记录进行持久化存储
       uni.setStorageSync('kw',JSON.stringify(this.historyList))
       
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      // 清空历史记录
      clean(){
        this.historyList=[]
        // 清空缓存里的相应数据
        // uni.setStorageSync('kw','[]')
        uni.removeStorageSync('kw')
        
      },
      // 点击搜索记录跳转页面
      togoGoodsList(item){
        uni.navigateTo({
          url:'../goods_list/goods_list?query='+item
        })
      }
    },
    computed:{
      // 要解决前后顺序(方法二)
      history(){
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index:100;
}
.sugg-list{
  padding: 0 10px;
  .sugg-item{
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    .goods_name{
      // 文本不允许换行(当行文本)
      white-space: nowrap;
      // 文本溢出后隐藏
      overflow: hidden;
      // 用三个点替换
      text-overflow: ellipsis;
    }
  }
}
.history-box{
  padding: 0 10px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
    margin-bottom: 5px;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    .uni-tag{
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
